package com.one.blocks.rbac.security;

import com.one.blocks.rbac.domain.SysUser;
import com.one.blocks.rbac.manager.SysUserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author <a href="mailto:idler41@163.con">linfuxin</a> created on 2023-08-12 20:11:51
 */
@Service
public class RbacUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserManager sysUserManager;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserManager.getByUsername(username);
        if (sysUser == null) {
            return null;
        }
        RbacUserDetails data = new RbacUserDetails();
        data.setSysUser(sysUser);
        data.setUserId(sysUser.getId());
        data.setAccessTime(LocalDateTime.now());
        data.setLoginTime(data.getAccessTime());
        // TODO 参数化
        data.setLoginExpireTime(data.getLoginTime().plusMinutes(30L));
        // 默认无密码过期时间
        data.setChangeExpireTime(LocalDateTime.MAX);
        return data;
    }

}
